package com.huawei.hms.framework.network.restclient.hwhttp;

import android.content.Context;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.common.hianalytics.HianalyticsHelper;
import com.huawei.hms.framework.network.restclient.hianalytics.CrashHandler;
import com.huawei.hms.framework.network.restclient.hwhttp.Request.Builder;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DNManager;
import com.huawei.hms.framework.network.util.ContextUtil;

public class HttpClientGlobalInstance {
    private static final String TAG = HttpClientGlobalInstance.class.getSimpleName();
    private static HttpClientGlobalInstance instance = new HttpClientGlobalInstance();
    private boolean bInit = false;
    private DNManager dnManager = DNManager.getInstance();
    private HttpClient httpClient;
    private String netSdkType;

    private HttpClientGlobalInstance() {
    }

    public static HttpClientGlobalInstance getInstance() {
        return instance;
    }

    public HttpClientGlobalInstance init(Context context) {
        synchronized (this) {
            if (this.bInit) {
            } else {
                this.bInit = true;
                ContextUtil.setContext(context);
                this.dnManager.init(ContextUtil.getContext(), null);
                new CrashHandler().init();
            }
        }
        return this;
    }

    public void connect(String str, int i, Callback callback) {
        connect(getHttpClient(), str, i, callback);
    }

    public void connect(HttpClient httpClient, String str, int i, Callback callback) {
        if (callback == null) {
            Logger.w(TAG, "callback is null connect no effect");
            return;
        }
        Request build = new Builder().url(str).method(HttpContants.HTTP_METHOD_GET).onlyConnect(true).build();
        for (int i2 = 0; i2 < i; i2++) {
            httpClient.newSubmit(build).enqueue(callback);
        }
    }

    public synchronized HttpClient getHttpClient() {
        HttpClient httpClient;
        if (ContextUtil.getContext() == null) {
            httpClient = null;
        } else {
            if (this.httpClient == null) {
                this.httpClient = new HttpClient.Builder().isReportable(true).enableQuic(false).build();
            }
            httpClient = this.httpClient;
        }
        return httpClient;
    }

    @Deprecated
    public void setNetSDKType(String str) {
        this.netSdkType = str;
    }

    @Deprecated
    public String getNetSdkType() {
        return this.netSdkType;
    }

    @Deprecated
    public HttpClientGlobalInstance enableHAFullReport() {
        return this;
    }

    public HttpClientGlobalInstance setHaTag(String str) {
        HianalyticsHelper.getInstance().setHaTag(str);
        return this;
    }
}
